//
//  GCTopicDetailViewController.m
//  GoldChampion
//
//  Created by Mac on 2018/4/12.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "GCTopicDetailViewController.h"
#import "GCTopicCollectionViewCell.h"
#import "GCTopicHeaderCollectionHeader.h"
#import "UICollectionView+STNoresult.h"
@interface GCTopicDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, weak)  UICollectionView    *collectionView;
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation GCTopicDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"#指尖上的大运河#";
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    UICollectionViewFlowLayout *flowout = [[UICollectionViewFlowLayout alloc] init];
    flowout.minimumLineSpacing = 1;
    flowout.minimumInteritemSpacing = 1;
    flowout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    flowout.headerReferenceSize = CGSizeMake(UIScreenWidth, topicHeaderCollectionHeaderHeight);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowout];
    collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
     [self.view addSubview:collectionView];
    [collectionView registerClass:[GCTopicCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [collectionView registerClass:[GCTopicHeaderCollectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    self.collectionView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, flowout.headerReferenceSize.height + 100, UIScreenWidth, 400) title:@"" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendTopListRequest];
    }];
    [self.collectionView.mj_header beginRefreshing];
    
}
#pragma --mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(TopicCollectionViewCellWidth, TopicCollectionViewCellWidth);
}
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    GCTopicHeaderCollectionHeader * heder = [self.collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    return heder;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GCTopicCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    GCItemModel * model = self.dataSouce[indexPath.row];
    [item.iconButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.img] forState:UIControlStateNormal];
    return item;
}
#pragma --mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendTopListRequest{
    NSArray *array = [GCItemModel mj_objectArrayWithFilename:@"1.plist"];
    [TMUtils debugSimulationNetWorkWithHadle:^(NSInteger afterTime) {
        self.dataSouce = [array mutableCopy];
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    }];
    
}
@end
